# MAKEFLAGS += -s
CROSS_COMPILE:=/home/duan/00_works/3_document/Ingenic/mips-gcc540-glibc222-r3.3.7.mxu2.mem03/bin/mips-linux-gnu-
CC = $(CROSS_COMPILE)gcc
CPP = $(CROSS_COMPILE)g++
AR = $(CROSS_COMPILE)gcc-ar
STRIP = $(CROSS_COMPILE)strip

# muclibc, mglibc
LIBTYPE ?= muclibc
# nv12, rgba/bgra
INTYPE ?= rgba
# static, dynamic
LIBSTATUS ?= dynamic

# 获取 Makefile 所在目录
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
parent_dir := $(dir $(patsubst %/,%,$(dir $(mkfile_path))))
CURRENT_DIR := $(dir $(patsubst %/,%,$(dir $(parent_dir))))
OBJS_PATH = $(CURRENT_DIR)/objs

TNNA_LIB_PATH = $(CURRENT_DIR)/InferenceKit/uranus/tnna/
XB_LIB_PATH = $(CURRENT_DIR)/xb_libs/

DEF_OPTTIONS= -std=c++14 -mfp32 -Wall -EL -O3 -Os -march=mips32r2 -flax-vector-conversions -lpthread -lrt -ldl -lm -fPIC -flto -Wl,-Map=object.map,--cref,--gc-section

CXXFLAGS := $(DEFEINE_OPT) -ffunction-sections -fdata-sections -fno-exceptions

ifeq ($(INTYPE), nv12)
	CXXFLAGS += -DURANUS_NV12
endif

ifeq ($(LIBTYPE), muclibc)
    CXXFLAGS += -muclibc
	LIB_PATH = $(TNNA_LIB_PATH)/540/lib/uclibc
    ifeq ($(LIBSTATUS), dynamic)
	    TARGET = catface_detect_run
    else
	    TARGET = catface_detect_run
    endif
else
	LIB_PATH = $(TNNA_LIB_PATH)/540/lib/glibc
    ifeq ($(LIBSTATUS), dynamic)
	    TARGET = load_mgk_glibc
    else
	    TARGET = load_mgk_glibc_static
    endif
endif 

INCLUDES := -I$(TNNA_LIB_PATH) -I$(TNNA_LIB_PATH)/include -I$(CURRENT_DIR)/include

OBJS := inference_yolo.o
	
%.o:%.cpp
	$(CPP) $(DEF_OPTTIONS) $(INCLUDES) $(CXXFLAGS) -o $@ $^ -c -ffunction-sections -fdata-sections -fno-exceptions

$(TARGET):$(OBJS)
    ifeq ($(LIBSTATUS), dynamic)
		$(CPP) $(CXXFLAGS) $^ -o $@ $(INCLUDES) -Wl,-rpath-link,$(LIB_PATH) $(XB_LIB_PATH)/lib_catface_recog.so  $(LIB_PATH)/liburanus_tensor.so $(LIB_PATH)/liburanus_core.so $(LIB_PATH)/liburanus_extend.so $(LIB_PATH)/libaie.so -Wl,-no-whole-archive -Wl,-gc-sections -fno-exceptions -Wl,-export-dynamic
    else
		$(CPP) $(CXXFLAGS) $^ -o $@ $(INCLUDES) -Wl,-whole-archive $(XB_LIB_PATH)/lib_catface_recog.so  $(LIB_PATH)/liburanus_tensor.a $(LIB_PATH)/liburanus_core.a $(LIB_PATH)/liburanus_extend.a $(LIB_PATH)/libaie.a -Wl,-no-whole-archive -Wl,-gc-sections -fno-exceptions -Wl,-export-dynamic -ldl
    endif


all : $(TARGET)

.PHONY: all clean
clean:
	rm -rf $(OBJS) $(MODEL_O) $(TARGET) $(MODEL_LIB)
